package com.outstanding.his.treatment.facade;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.outstanding.his.patient.domain.Patient;
import com.outstanding.his.patient.service.IPatientService;
import com.outstanding.his.revisit.domain.Revisit;
import com.outstanding.his.revisit.service.IRevisitService;
import com.outstanding.his.treatment.domain.ChineseMedicine;
import com.outstanding.his.treatment.domain.Treatment;
import com.outstanding.his.treatment.domain.WesternMedicine;
import com.outstanding.his.treatment.service.ITreatmentService;

@Controller
@SessionAttributes( { "patient", "treatment" })
public class TreatmentController {

	private ITreatmentService treatmentService;

	private IPatientService patientService;

	private IRevisitService revisitService;

	public void setTreatmentService(ITreatmentService treatmentService) {
		this.treatmentService = treatmentService;
	}

	public void setPatientService(IPatientService patientService) {
		this.patientService = patientService;
	}

	public void setRevisitService(IRevisitService revisitService) {
		this.revisitService = revisitService;
	}

	@RequestMapping("/treatment/new.do")
	public String treatmentNewForm(
			@RequestParam("patientId") Integer patientId, Model model) {
		Patient patient = patientService.getPatient(patientId);
		Treatment treatment = new Treatment();
		model.addAttribute("treatment", treatment);
		model.addAttribute("patient", patient);
		return "/treatment/new";
	}

	@RequestMapping("/treatment/update.do")
	public String treatmentUpdate(
			@ModelAttribute("treatment") Treatment treatment,
			@ModelAttribute("patient") Patient patient,
			HttpServletRequest request) throws ParseException {

		String treatTimeStr = request.getParameter("treatmentTimeStr");

		if (!treatTimeStr.equals("") && treatTimeStr != null) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			treatment.setTreatmentTime(dateFormat.parse(treatTimeStr));
		} else {
			treatment.setTreatmentTime(new Date());
		}

		String[] westernMedicineNames = request
				.getParameterValues("westernMedicineName");
		String[] westernMedicineDoses = request
				.getParameterValues("westernMedicineDose");
		String[] morningWesternMedicineDoses = request
				.getParameterValues("morningWesternMedicineDose");
		String[] noonWesternMedicineDoses = request
				.getParameterValues("noonWesternMedicineDose");
		String[] nightWesternMedicineDoses = request
				.getParameterValues("nightWesternMedicineDose");

		String[] chineseMedicineNames = request
				.getParameterValues("chineseMedicineName");
		String[] chineseMedicineDoses = request
				.getParameterValues("chineseMedicineDose");
		String[] morningChineseMedicineDoses = request
				.getParameterValues("morningChineseMedicineDose");
		String[] noonChineseMedicineDoses = request
				.getParameterValues("noonChineseMedicineDose");
		String[] nightChineseMedicineDoses = request
				.getParameterValues("nightChineseMedicineDose");

		List<WesternMedicine> westernMedicines = treatment
				.getWesternMedicines();
		int westernMedicineSize = westernMedicines.size();
		for (int i = 0; i < westernMedicineSize; i++) {
			this.treatmentService
					.deleteWesternMedicine(westernMedicines.get(i));
		}
		treatment.getWesternMedicines().clear();
		// patient.getTreatments().clear();

		for (int i = 0; i < westernMedicineNames.length; i++) {
			WesternMedicine westernMedicine = new WesternMedicine();
			westernMedicine.setWesternMedicineName(westernMedicineNames[i]);
			westernMedicine.setWesternMedicineDose(westernMedicineDoses[i]);
			westernMedicine
					.setMorningWesternMedicineDose(morningWesternMedicineDoses[i]);
			westernMedicine
					.setNoonWesternMedicineDose(noonWesternMedicineDoses[i]);
			westernMedicine
					.setNightWesternMedicineDose(nightWesternMedicineDoses[i]);
			treatment.getWesternMedicines().add(westernMedicine);
		}

		List<ChineseMedicine> chineseMedicines = treatment
				.getChineseMedicines();
		int chineseMedicineSize = chineseMedicines.size();
		for (int i = 0; i < chineseMedicineSize; i++) {
			this.treatmentService
					.deleteChineseMedicine(chineseMedicines.get(i));
		}
		treatment.getChineseMedicines().clear();

		for (int i = 0; i < chineseMedicineNames.length; i++) {
			ChineseMedicine chineseMedicine = new ChineseMedicine();
			chineseMedicine.setChineseMedicineName(chineseMedicineNames[i]);
			chineseMedicine.setChineseMedicineDose(chineseMedicineDoses[i]);
			chineseMedicine
					.setMorningChineseMedicineDose(morningChineseMedicineDoses[i]);
			chineseMedicine
					.setNoonChineseMedicineDose(noonChineseMedicineDoses[i]);
			chineseMedicine
					.setNightChineseMedicineDose(nightChineseMedicineDoses[i]);
			treatment.getChineseMedicines().add(chineseMedicine);
		}

		treatment = treatmentService.updateTreatment(treatment);
		// patient.getTreatments().add(treatment);
		// patientService.updatePatient(patient);

		return "redirect:/treatment/view.do?treatmentId="
				+ treatment.getTreatmentId();

	}

	@RequestMapping("/treatment/submit.do")
	public String treatmentSubmit(
			@ModelAttribute("treatment") Treatment treatment,
			@ModelAttribute("patient") Patient patient,
			HttpServletRequest request) throws ParseException {

		treatment = treatmentService.submitTreatment(treatment);

		String treatTimeStr = request.getParameter("treatmentTimeStr");
		if (!treatTimeStr.equals("") && treatTimeStr != null) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			treatment.setTreatmentTime(dateFormat.parse(treatTimeStr));
		} else {
			treatment.setTreatmentTime(new Date());
		}

		String[] westernMedicineNames = request
				.getParameterValues("westernMedicineName");
		String[] westernMedicineDoses = request
				.getParameterValues("westernMedicineDose");
		String[] morningWesternMedicineDoses = request
				.getParameterValues("morningWesternMedicineDose");
		String[] noonWesternMedicineDoses = request
				.getParameterValues("noonWesternMedicineDose");
		String[] nightWesternMedicineDoses = request
				.getParameterValues("nightWesternMedicineDose");

		String[] chineseMedicineNames = request
				.getParameterValues("chineseMedicineName");
		String[] chineseMedicineDoses = request
				.getParameterValues("chineseMedicineDose");
		String[] morningChineseMedicineDoses = request
				.getParameterValues("morningChineseMedicineDose");
		String[] noonChineseMedicineDoses = request
				.getParameterValues("noonChineseMedicineDose");
		String[] nightChineseMedicineDoses = request
				.getParameterValues("nightChineseMedicineDose");

		List<WesternMedicine> westernMedicines = treatment
				.getWesternMedicines();
		int westernMedicineSize = westernMedicines.size();
		for (int i = 0; i < westernMedicineSize; i++) {
			this.treatmentService
					.deleteWesternMedicine(westernMedicines.get(i));
		}
		treatment.getWesternMedicines().clear();
		// patient.getTreatments().clear();

		for (int i = 0; i < westernMedicineNames.length; i++) {
			WesternMedicine westernMedicine = new WesternMedicine();
			westernMedicine.setWesternMedicineName(westernMedicineNames[i]);
			westernMedicine.setWesternMedicineDose(westernMedicineDoses[i]);
			westernMedicine
					.setMorningWesternMedicineDose(morningWesternMedicineDoses[i]);
			westernMedicine
					.setNoonWesternMedicineDose(noonWesternMedicineDoses[i]);
			westernMedicine
					.setNightWesternMedicineDose(nightWesternMedicineDoses[i]);
			treatment.getWesternMedicines().add(westernMedicine);
		}

		List<ChineseMedicine> chineseMedicines = treatment
				.getChineseMedicines();
		int chineseMedicineSize = chineseMedicines.size();
		for (int i = 0; i < chineseMedicineSize; i++) {
			this.treatmentService
					.deleteChineseMedicine(chineseMedicines.get(i));
		}
		treatment.getChineseMedicines().clear();

		for (int i = 0; i < chineseMedicineNames.length; i++) {
			ChineseMedicine chineseMedicine = new ChineseMedicine();
			chineseMedicine.setChineseMedicineName(chineseMedicineNames[i]);
			chineseMedicine.setChineseMedicineDose(chineseMedicineDoses[i]);
			chineseMedicine
					.setMorningChineseMedicineDose(morningChineseMedicineDoses[i]);
			chineseMedicine
					.setNoonChineseMedicineDose(noonChineseMedicineDoses[i]);
			chineseMedicine
					.setNightChineseMedicineDose(nightChineseMedicineDoses[i]);
			treatment.getChineseMedicines().add(chineseMedicine);
		}

		// treatment = treatmentService.updateTreatment(treatment);

		patient.getTreatments().add(treatment);
		// 如果是第一次治疗，增加回访记录
		if (patient.getFirstTreatmentTime() == null) {
			patient.setFirstTreatmentTime(treatment.getTreatmentTime());
			Revisit revisit = new Revisit();
			Date revisitTime = new Date(treatment.getTreatmentTime()
					.getTime()
					+ 3 * 24 * 60 * 60 * 1000);
			Date nextRevisitTime = new Date(treatment.getTreatmentTime()
					.getTime()
					+ 8 * 24 * 60 * 60 * 1000);
			revisit.setRevisitTime(revisitTime);
			revisit.setNextRevisitTime(nextRevisitTime);		
			patient.getRevisits().add(revisit);
		}

		patientService.updatePatient(patient);

		return "redirect:/treatment/view.do?treatmentId="
				+ treatment.getTreatmentId();
	}

	@RequestMapping("/treatment/list.do")
	public String treatmentList(@RequestParam("patientId") Integer patientId,
			Model model) {
		Patient patient = patientService.getPatient(patientId);
		model.addAttribute("patient", patient);
		return "/treatment/list";

	}

	@RequestMapping("/treatment/view.do")
	public String treatmentView(@ModelAttribute("patient") Patient patient,
			@RequestParam("treatmentId") Integer treatmentId, Model model) {
		Treatment treatment = treatmentService.getTreatment(treatmentId);
		model.addAttribute("treatment", treatment);
		return "/treatment/view";
	}

	@RequestMapping(value = "/treatment/edit.do", method = RequestMethod.GET)
	public String treatmentEdit(@ModelAttribute("patient") Patient patient,
			@RequestParam("treatmentId") Integer treatmentId, Model model) {
		Treatment treatment = treatmentService.getTreatment(treatmentId);
		model.addAttribute("treatment", treatment);
		return "/treatment/edit";
	}

}
